package cn.iocoder.yudao.module.promotion.service.prizedraw;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawNumberPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawNumberSaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.prizedraw.PrizeDrawNumberDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.prizedraw.PrizeDrawNumberMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.PRIZE_DRAW_NUMBER_NOT_EXISTS;

/**
 * 用户抽奖次数流水 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PrizeDrawNumberServiceImpl implements PrizeDrawNumberService {

    @Resource
    private PrizeDrawNumberMapper prizeDrawNumberMapper;

    @Override
    public Long createPrizeDrawNumber(PrizeDrawNumberSaveReqVO createReqVO) {
        // 插入
        PrizeDrawNumberDO prizeDrawNumber = BeanUtils.toBean(createReqVO, PrizeDrawNumberDO.class);
        prizeDrawNumberMapper.insert(prizeDrawNumber);
        // 返回
        return prizeDrawNumber.getPrizeDrawNumberId();
    }

    @Override
    public void updatePrizeDrawNumber(PrizeDrawNumberSaveReqVO updateReqVO) {
        // 校验存在
        validatePrizeDrawNumberExists(updateReqVO.getPrizeDrawNumberId());
        // 更新
        PrizeDrawNumberDO updateObj = BeanUtils.toBean(updateReqVO, PrizeDrawNumberDO.class);
        prizeDrawNumberMapper.updateById(updateObj);
    }

    @Override
    public void deletePrizeDrawNumber(Long id) {
        // 校验存在
        validatePrizeDrawNumberExists(id);
        // 删除
        prizeDrawNumberMapper.deleteById(id);
    }

    @Override
        public void deletePrizeDrawNumberListByIds(List<Long> ids) {
        // 校验存在
        validatePrizeDrawNumberExists(ids);
        // 删除
        prizeDrawNumberMapper.deleteByIds(ids);
        }

    private void validatePrizeDrawNumberExists(List<Long> ids) {
        List<PrizeDrawNumberDO> list = prizeDrawNumberMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PRIZE_DRAW_NUMBER_NOT_EXISTS);
        }
    }

    private void validatePrizeDrawNumberExists(Long id) {
        if (prizeDrawNumberMapper.selectById(id) == null) {
            throw exception(PRIZE_DRAW_NUMBER_NOT_EXISTS);
        }
    }

    @Override
    public PrizeDrawNumberDO getPrizeDrawNumber(Long id) {
        return prizeDrawNumberMapper.selectById(id);
    }

    @Override
    public PageResult<PrizeDrawNumberDO> getPrizeDrawNumberPage(PrizeDrawNumberPageReqVO pageReqVO) {
        return prizeDrawNumberMapper.selectPage(pageReqVO);
    }

}